﻿#region Namespace Inclusions
using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Timers;

using SCI_actuator.Properties;
#endregion

namespace SCI_actuator
{
    public partial class hermite_curve : Form
    {
        private menu_control frmain;
        private xmlrw scixml;
        public hermite_curve(menu_control fr)
        {
            InitializeComponent();
            this.frmain = fr;
            scixml = new xmlrw();
            txtcposiH.Text = Settings.Default.posiH;
            txtcposiL.Text = Settings.Default.posiL;
            txtTdeltaH.Text = Settings.Default.TdeltaH;
            txtTdeltaL.Text = Settings.Default.TdeltaL;
            txtdelta.Text = Settings.Default.cdelta;
            txtOUTspeedH.Text = Settings.Default.speedH;
            txtOUTspeedL.Text = Settings.Default.speedL;
        }

        private void truebtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(scixml.Read_ServoID() + "12" + "02" + constant.REG_CURVE_POSITION_HI + txtcposiH.Text
            + constant.REG_CURVE_POSITION_LO + txtcposiL.Text + constant.REG_CURVE_DELTA_HI + txtTdeltaH.Text 
            + constant.REG_CURVE_DELTA_LO + txtTdeltaL.Text + constant.REG_OUT_VELOCITY_HI + txtOUTspeedH.Text + constant.REG_OUT_VELOCITY_LO
            + txtOUTspeedL.Text + constant.REG_DELTA + txtdelta.Text + constant.REG_CURVE_APPEND + "01");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + scixml.Read_ServoID() + "12" + "02" + constant.REG_CURVE_POSITION_HI + txtcposiH.Text
            + constant.REG_CURVE_POSITION_LO + txtcposiL.Text + constant.REG_CURVE_DELTA_HI + txtTdeltaH.Text
            + constant.REG_CURVE_DELTA_LO + txtTdeltaL.Text + constant.REG_OUT_VELOCITY_HI + txtOUTspeedH.Text + constant.REG_OUT_VELOCITY_LO
            + txtOUTspeedL.Text + constant.REG_DELTA + txtdelta.Text + constant.REG_CURVE_APPEND + "01" + sum_str;
            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
            Settings.Default.posiH = txtcposiH.Text;
            Settings.Default.posiL = txtcposiL.Text;
            Settings.Default.TdeltaH = txtTdeltaH.Text;
            Settings.Default.TdeltaL = txtTdeltaL.Text;
            Settings.Default.cdelta = txtdelta.Text;
            Settings.Default.Save();
        }

    }
}  